<template>
  <!-- 立即搜索 -->
  <div class="addincomedata">
    <Inoutcome :tableData="tableData" @jumpinoutcome="getData" :currentpage="currentpage" @setmsg="setmsg"/>
    <router-view></router-view>

    <!-- 页码 -->
    <Paging
      :class="pg"
      :total="totalSize"
      @getLimit="getLimit"
      @getPage="getPage"
    />
  </div>
</template>

<script>
import Inoutcome from "../../components/inoutcome.vue"
import { getAddInOutcomeAxios, addCustomerDataAxios } from "../../http/apis/moneyApi/index"
import Paging from "../../components/Paging.vue";
import { timeFormat } from "../../utils/utils";

export default {
  name: "AddIncomeData",
  components: {
    Inoutcome,
    Paging,
  },
  data() {
    return {
      search: "",
      tableData: [],
      currentpage: 1,

      pg: "page",
      currPage: 1, //当前页
      pageSize: 3, //页数
      totalSize: 0, // 总共有多少条数据
    };
  },
  created() {
    this.formatPage()
  },
  methods: {
    getData(row) {
      getAddInOutcomeAxios(this.currentpage)
      .then(res => {
        console.log("查询成功：", res.data.data);
        this.$router.push({ path: "/money/addinoutcome", query: { detailmsg: row,querymsg: res.data.data, currentpage: this.currentpage} });
      })
      .catch(err => {
        console.log("查询失败：", err);
      })
    },
    setmsg() {
      getAddInOutcomeAxios(this.currentpage)
      .then(res => {
        console.log("查询成功：", res.data.data);
        this.$router.push({ path: "/money/addinoutcome", query: { detailmsg: null,querymsg: res.data.data,currentpage: this.currentpage} });
      })
      .catch(err => {
        console.log("查询失败：", err);
      })
    }, 
    // 得到一页有多少条数据
    getLimit(val) {
      this.pageSize = val;
      this.formatPage();
    },
    getPage(val) {
      this.currPage = val;
      this.formatPage(); // 重新发起请求，更新当前值
    },
    
    formatPage() {
      this.tableData = [];
      let data = {
        currentPage: this.currPage,
        pageSize: this.pageSize,
        data: {},
      };
      addCustomerDataAxios(data)
        .then((res) => {
          if (res.data.code == 200) {
            console.log("查询成功：", res.data.data);
            this.totalSize = res.data.data.totalSize;
            let arr = res.data.data.list;
            arr.forEach((item) => {
              let obj = {
                money_id: item.customer.customer_id,
                employee_id: item.employee_id,
                customname: item.customer.customer_name,
                customtype: '',
                concatperson: item.customer.contact[0].contact_name,
                phone: item.customer.contact[0].contact_phone,
                QQ: item.customer.contact[0].contact_qq,
                inputtime: timeFormat(
                  item.customer.customer_records.import_date
                ),
                salesman: item.employee.employee_name,
              };
              //计算每个分类有多少条
              this.tableData.push(obj);
            });
          }
        })
        .catch((err) => {
          console.log("查询失败：", err);
        });
    },
  }
};
</script>

<style>

</style>